import Base from 'models/base';

import SystemUtil from 'utils/systemUtil';

const regionFields = ['provincesCode', 'citiesCode', 'areasCode', 'streetsCode', 'villagesCode'];
export default class BranchForm extends Base {
  constructor(branch) {
    super();
    if (branch === undefined) {
      branch = {
        status: 1
      };
      this.regionData = [];
      this.majorTypeId = '';
      this.branchLicense = [];
      this.branchHeadImg = [];
    } else {
      this.branchId = branch.branchId;
      this.majorBranchPartnerId = branch.majorBranchPartnerId;

      let majorTypeId = branch.majorTypeId;
      if (SystemUtil.isEmpty(majorTypeId)) {
        majorTypeId = '';
      }
      this.majorTypeId = majorTypeId;

      let branchLicense = branch.branchLicense;
      if (SystemUtil.isEmpty(branchLicense)) {
        branchLicense = [];
      } else {
        branchLicense = [branchLicense];
      }
      this.branchLicense = branchLicense;

      let branchHeadImg = branch.branchHeadImg;
      if (SystemUtil.isEmpty(branchHeadImg)) {
        branchHeadImg = [];
      } else {
        branchHeadImg = [branchHeadImg];
      }
      this.branchHeadImg = branchHeadImg;

      let regionData = [];
      for (let i = 0; i < regionFields.length; i++) {
        const field = regionFields[i];
        let value = branch[field];
        if (!SystemUtil.isEmpty(value)) {
          regionData.push(value);
        }
      }
      this.regionData = regionData;
    }
    let fields = ['branchName', 'status', 'branchPhone', 'branchAddress', 'branchBankNo', 'branchBankInfo', 'branchWechatMchId', 'majorName'];
    this.initObject(branch, fields);

    let lng = branch.longitude;
    let lat = branch.latitude;
    let position;
    if (SystemUtil.isEmpty(lng) || SystemUtil.isEmpty(lat)) {
      position = [];
    } else {
      lat = Number(lat);
      lng = Number(lng);
      position = [lat, lng];
    }
    this.position = position;
  }

  checkValid() {
    let fields = ['branchName', 'branchHeadImg', 'majorTypeId', 'branchPhone', 'position', 'branchAddress', 'regionData', 'branchLicense',
      'branchBankNo', 'branchBankInfo', 'branchWechatMchId'
    ];

    let code = this.checkRequired(fields);
    if (code !== '') {
      return code;
    }

    let branchBankNo = this.branchBankNo;
    let reg = /^[1-9]\d{12,18}$/;
    if (!reg.test(branchBankNo)) {
      return 'bankNoNotTest';
    }
    return '';
  }

  getObject() {
    let postData = {};
    let fields = ['branchName', 'majorTypeId', 'branchPhone', 'branchAddress', 'branchBankNo', 'branchBankInfo', 'branchWechatMchId', 'status'];
    this.setPostData(postData, fields, true);

    let position = this.position;
    let [lat, lng] = position;
    postData.longitude = lng;
    postData.latitude = lat;

    let regionData = this.regionData;
    for (let i = 0; i < regionFields.length; i++) {
      const field = regionFields[i];
      let value = regionData[i];
      postData[field] = value;
    }

    let branchLicense = this.branchLicense;
    postData.branchLicense = branchLicense[0];

    let branchHeadImg = this.branchHeadImg;
    postData.branchHeadImg = branchHeadImg[0];

    let branchId = this.branchId;
    if (branchId !== undefined) {
      postData.branchId = branchId;
      postData.majorBranchPartnerId = this.majorBranchPartnerId;
    }
    return postData;
  }
}
